const mongoose = require("mongoose")
const jinguanModel = mongoose.model("jinguan")

module.exports.addLunbo = async (data) => {
    return await jinguanModel.create({imgType: "1", imgUrl: data})
}

module.exports.getLunbo = async () => {
    return await jinguanModel.find({imgType: 1 })
}

module.exports.deleteLunbo = async ({id}) => {
    return await jinguanModel.deleteOne({_id: id})
}

module.exports.addDetail = async ({titleImg, name, classify, description, detailImg}) => {
    return await jinguanModel.create({imgType: "2", imgUrl: titleImg, name:name, classify:classify, description: description, detailImg: detailImg})
    // return await jinguanModel.create({imgType: 2, imgUrl: })
}

module.exports.deleteDetail = async ({id}) => {
    return await jinguanModel.deleteOne({_id: id})
}

module.exports.updateDetail = async ({id, titleImg, name, classify, description, detailImg}) => {
    await jinguanModel.deleteOne({_id: id})
    return  await jinguanModel.create({imgType: "2", imgUrl: titleImg, name:name, classify:classify, description: description, detailImg: detailImg})
}

module.exports.getDetail = async ({index,type}) => {
    let count,data;
    if(type === undefined || type === ''){
        count = await jinguanModel.find({imgType: 2  }).count();
        data = await jinguanModel.find({imgType: 2 }).sort({"_id":1}).skip(index * 5).limit(5)
    }else {
        count = await jinguanModel.find({imgType: 2 ,classify:type }).count();
        data = await jinguanModel.find({imgType: 2 ,classify:type }).sort({"_id":1}).skip(index * 5).limit(5)
    }
    const reDate = {
        count: count,
        index: index,
        data:data,
    }
    return reDate
}
